/**
 * 
 */
package com.mbengue.corp.apr.ra.dao.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.mbengue.corp.apr.ra.core.Article;
import com.mbengue.corp.apr.ra.core.Membre;
import com.mbengue.corp.apr.ra.dao.ArticleDao;
import com.mbengue.corp.apr.ra.util.HibernateUtil;

/**
 * @author MMB11447
 *
 */
@SuppressWarnings("unchecked")
public class ArticleDaoImpl implements ArticleDao, Serializable {

	private static final long serialVersionUID = 1L;
	private Session session;
	
	public ArticleDaoImpl() {
		session = HibernateUtil.getSessionFactory();
	}
	
	/* (non-Javadoc)
	 * @see com.mbengue.corp.apr.ra.dao.ArticleDao#ajouterArticle(com.mbengue.corp.apr.ra.core.Article)
	 */
	public void ajouterArticle(Article article) throws HibernateException {
		Transaction transaction = session.beginTransaction();
		try {
			session.save(article);
			transaction.commit();
		} catch (Exception e) {
			transaction.rollback();
			throw new HibernateException(e);
		} finally {
			session.flush();
			session.close();
		}
	}

	/* (non-Javadoc)
	 * @see com.mbengue.corp.apr.ra.dao.ArticleDao#trouverArticle(java.lang.String)
	 */
	public Article trouverArticle(String titre) throws HibernateException {
		Criteria criteria = session.createCriteria(Article.class);
		criteria.add(Restrictions.eq("titre", titre));
		
		return (Article) criteria.uniqueResult();
	}

	/* (non-Javadoc)
	 * @see com.mbengue.corp.apr.ra.dao.ArticleDao#modifierArticle(com.mbengue.corp.apr.ra.core.Article)
	 */
	public void modifierArticle(Article article) throws HibernateException {
		Transaction transaction = session.beginTransaction();
		try {
			session.update(article);
			transaction.commit();
		} catch (Exception e) {
			transaction.rollback();
			throw new HibernateException(e);
		} finally {
			session.flush();
			session.close();
		}
	}

	/* (non-Javadoc)
	 * @see com.mbengue.corp.apr.ra.dao.ArticleDao#tousLesArticles()
	 */
	public List<Article> tousLesArticles() throws HibernateException {
		Criteria criteria = session.createCriteria(Article.class);
		
		return criteria.list();
	}

	/* (non-Javadoc)
	 * @see com.mbengue.corp.apr.ra.dao.ArticleDao#lesArticlesD1Auteur(com.mbengue.corp.apr.ra.core.Membre)
	 */
	public List<Article> lesArticlesD1Auteur(Membre membre) throws HibernateException {
		Query query = session.getNamedQuery("articlesD1Auteur");
		query.setString(1, membre.getEmail());
		
		return query.list();
	}

	/* (non-Javadoc)
	 * @see com.mbengue.corp.apr.ra.dao.ArticleDao#lesArticlesRecents()
	 */
	public List<Article> lesArticlesRecents() throws HibernateException {
		int maxResult = 6;
		Criteria criteria = session.createCriteria(Article.class);
		criteria.add(Restrictions.le("dateDeCreation", new Date()));
		criteria.addOrder(Order.desc("dateDeCreation"));
		criteria.setMaxResults(maxResult);
		
		return criteria.list();
	}

	/* (non-Javadoc)
	 * @see com.mbengue.corp.apr.ra.dao.ArticleDao#tousLesPvDeReunion()
	 */
	public List<Article> tousLesPvDeReunion() throws HibernateException {
		Query query = session.getNamedQuery("pvReunion");
		return query.list();
	}

}
